package com.baizhi.controller;


import com.baizhi.constants.RedisPrefix;
import com.baizhi.dto.CommentDTO;
import com.baizhi.entity.User;
import com.baizhi.po.CommentsPO;
import com.baizhi.service.CommentService;
import com.baomidou.mybatisplus.extension.api.ApiController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 评论(Comment)表控制层
 *
 * @author makejava
 * @since 2021-09-28 14:40:40
 */
@RestController
@RequestMapping
@RefreshScope
public class CommentController extends ApiController {
    /**
     * 服务对象
     */
    @Resource
    private CommentService commentService;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 当前视频的所有评论
     *
     * @param videoId
     * @param page
     * @param per_page
     */
    @GetMapping("/videos/{video_id}/comments")
    public CommentsPO queryByVideo(@PathVariable("video_id") Integer videoId,
                                   @RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
                                   @RequestParam(value = "per_page", required = false, defaultValue = "1") Integer per_page) {
        CommentsPO commentsPO = commentService.queryAllByVideoId(page, per_page, videoId);
        return commentsPO;
    }

    /**
     * 添加评论
     *
     * @param token
     * @param videoId
     * @param commentDTO
     */
    @PostMapping("/videos/{video_id}/comments")
    public void add(@RequestParam("token") String token,
                    @PathVariable("video_id") Integer videoId,
                    @RequestBody CommentDTO commentDTO) {
        //获取当前用户
        User user = (User) redisTemplate.opsForValue().get(RedisPrefix.USER_TOKEN_PREFIX + token);
        Integer id = user.getId();
        //添加评论
        commentService.insert(id, videoId, commentDTO);
    }
}

